<template>
  <el-dialog
    :title="!dataForm.did ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="优惠券名称" prop="dname">
      <el-input v-model="dataForm.dname" placeholder="优惠券名称"></el-input>
    </el-form-item>
    <el-form-item label="优惠券类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="优惠券类型"></el-input>
    </el-form-item>
    <el-form-item label="满额限制" prop="man">
      <el-input v-model="dataForm.man" placeholder="满额限制"></el-input>
    </el-form-item>
    <el-form-item label="折扣金额" prop="zhe">
      <el-input v-model="dataForm.zhe" placeholder="折扣金额"></el-input>
    </el-form-item>
    <el-form-item label="折扣比例" prop="bi">
      <el-input v-model="dataForm.bi" placeholder="折扣比例"></el-input>
    </el-form-item>
    <el-form-item label="有效天数" prop="dateCount">
      <el-input v-model="dataForm.dateCount" placeholder="有效天数"></el-input>
    </el-form-item>
    <el-form-item label="可用数量" prop="keYong">
      <el-input v-model="dataForm.keYong" placeholder="可用数量"></el-input>
    </el-form-item>
    <el-form-item label="已领数量" prop="yiLing">
      <el-input v-model="dataForm.yiLing" placeholder="已领数量"></el-input>
    </el-form-item>
    <el-form-item label="已使用数量" prop="yiYong">
      <el-input v-model="dataForm.yiYong" placeholder="已使用数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          did: 0,
          dname: '',
          type: '',
          man: '',
          zhe: '',
          bi: '',
          dateCount: '',
          keYong: '',
          yiLing: '',
          yiYong: ''
        },
        dataRule: {
          dname: [
            { required: true, message: '优惠券名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '优惠券类型不能为空', trigger: 'blur' }
          ],
          man: [
            { required: true, message: '满额限制不能为空', trigger: 'blur' }
          ],
          zhe: [
            { required: true, message: '折扣金额不能为空', trigger: 'blur' }
          ],
          bi: [
            { required: true, message: '折扣比例不能为空', trigger: 'blur' }
          ],
          dateCount: [
            { required: true, message: '有效天数不能为空', trigger: 'blur' }
          ],
          keYong: [
            { required: true, message: '可用数量不能为空', trigger: 'blur' }
          ],
          yiLing: [
            { required: true, message: '已领数量不能为空', trigger: 'blur' }
          ],
          yiYong: [
            { required: true, message: '已使用数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.did = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.did) {
            this.$http({
              url: this.$http.adornUrl(`/car/discount/info/${this.dataForm.did}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dname = data.discount.dname
                this.dataForm.type = data.discount.type
                this.dataForm.man = data.discount.man
                this.dataForm.zhe = data.discount.zhe
                this.dataForm.bi = data.discount.bi
                this.dataForm.dateCount = data.discount.dateCount
                this.dataForm.keYong = data.discount.keYong
                this.dataForm.yiLing = data.discount.yiLing
                this.dataForm.yiYong = data.discount.yiYong
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/car/discount/${!this.dataForm.did ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'did': this.dataForm.did || undefined,
                'dname': this.dataForm.dname,
                'type': this.dataForm.type,
                'man': this.dataForm.man,
                'zhe': this.dataForm.zhe,
                'bi': this.dataForm.bi,
                'dateCount': this.dataForm.dateCount,
                'keYong': this.dataForm.keYong,
                'yiLing': this.dataForm.yiLing,
                'yiYong': this.dataForm.yiYong
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
